package com.example.videotimedemo;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.example.videotimedemo.app.App;
import com.example.videotimedemo.util.VideoUtils;

import java.util.HashMap;

import cn.jzvd.JZMediaSystem;
import cn.jzvd.JZUtils;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    public static final String videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
    private Handler mHandler;
    private JzvdStd jzvdStd;
    private static final String TAG = MainActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mHandler = new Handler();
        initView();
        initData();
    }

    private void initData() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                long time = VideoUtils.getPlayTime(videoUrl);
                Log.d("--time--", time + "");
                jzvdStd.totalTimeTextView.setText(JZUtils.stringForTime(time));
            }
        }, 200);
        getDuration(videoUrl);
    }

    public void getDuration(String url) {
        io.reactivex.Observable<Long> observable = io.reactivex.Observable.create(new ObservableOnSubscribe<Long>() {
            @Override
            public void subscribe(ObservableEmitter<Long> e) throws Exception {
                android.media.MediaMetadataRetriever metadataRetriever = new android.media.MediaMetadataRetriever();
                try {
                    if (url != null) {
                        HashMap<String, String> headers;
                        headers = new HashMap<>();
                        headers.put("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.4.2; zh-CN; MW-KW-001 Build/JRO03C) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/1.0.0.001 U4/0.8.0 Mobile Safari/533.1");
                        metadataRetriever.setDataSource(url, headers);
                    } else {
                        //mmr.setDataSource(mFD, mOffset, mLength);
                    }
                    long durationTime = Long.parseLong(metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));//时长(毫秒)
                    String width = metadataRetriever.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);//宽
                    String height = metadataRetriever.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);//高
                    e.onNext(durationTime);
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    metadataRetriever.release();
                }
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
        observable.subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Long time) {
                Log.d("--time--", time.toString());
                jzvdStd.totalTimeTextView.setText(JZUtils.stringForTime(time));
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void initView() {
        jzvdStd = findViewById(R.id.videoplayer);
        if (jzvdStd != null) {

            jzvdStd.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Bitmap bitmap = VideoUtils.GetFramePictures(videoUrl,jzvdStd.getCurrentPositionWhenPlaying());
                    Log.d(TAG,"视频封面为："+ bitmap);
                }
            },100);
             String proxyUrl = App.getProxy(this).getProxyUrl(videoUrl);
             jzvdStd.setUp(proxyUrl, "视频默认没有播放", JzvdStd.SCREEN_NORMAL, JZMediaSystem.class);
            jzvdStd.startVideo();
           // jzvdStd.postDelayed(() -> ),100);
            //VideoUtils.loadVideoScreenshot(this, videoUrl, jzvdStd.thumbImageView, jzvdStd.getDuration());
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.goOnPlayOnPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Jzvd.goOnPlayOnResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Jzvd.releaseAllVideos();
    }


}